#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
直接清空所有评估数据 - 无需交互
警告：执行后立即删除所有数据！
"""

import pymysql
import sys

# 数据库配置
DB_CONFIG = {
    'host': 'localhost',
    'port': 3306,
    'user': 'flask_dev',
    'password': 'dev123456',
    'database': 'flask_local',
    'charset': 'utf8mb4'
}

def main():
    print("\n" + "="*50)
    print("[+] 开始清空所有评估数据...")
    print("="*50 + "\n")
    
    try:
        connection = pymysql.connect(**DB_CONFIG)
        cursor = connection.cursor()
        
        # 显示删除前统计
        print("[1/5] 查询当前数据...")
        cursor.execute("SELECT COUNT(*) FROM scale_assessment")
        assessment_count = cursor.fetchone()[0]
        cursor.execute("SELECT COUNT(*) FROM scale_hamd17")
        hamd_count = cursor.fetchone()[0]
        cursor.execute("SELECT COUNT(*) FROM scale_hama")
        hama_count = cursor.fetchone()[0]
        cursor.execute("SELECT COUNT(*) FROM scale_ymrs")
        ymrs_count = cursor.fetchone()[0]
        
        print(f"  - 评估记录: {assessment_count} 条")
        print(f"  - HAMD记录: {hamd_count} 条")
        print(f"  - HAMA记录: {hama_count} 条")
        print(f"  - YMRS记录: {ymrs_count} 条")
        
        # 删除数据
        print("\n[2/5] 禁用外键检查...")
        cursor.execute("SET FOREIGN_KEY_CHECKS = 0")
        
        print("[3/5] 删除所有评估数据...")
        cursor.execute("DELETE FROM scale_hamd17")
        cursor.execute("DELETE FROM scale_hama")
        cursor.execute("DELETE FROM scale_ymrs")
        cursor.execute("DELETE FROM scale_assessment")
        
        print("[4/5] 重置自增ID...")
        cursor.execute("ALTER TABLE scale_assessment AUTO_INCREMENT = 1")
        cursor.execute("ALTER TABLE scale_hamd17 AUTO_INCREMENT = 1")
        cursor.execute("ALTER TABLE scale_hama AUTO_INCREMENT = 1")
        cursor.execute("ALTER TABLE scale_ymrs AUTO_INCREMENT = 1")
        
        print("[5/5] 启用外键检查...")
        cursor.execute("SET FOREIGN_KEY_CHECKS = 1")
        
        # 提交更改
        connection.commit()
        
        # 验证删除结果
        print("\n" + "="*50)
        print("[+] 验证删除结果...")
        print("="*50)
        cursor.execute("SELECT COUNT(*) FROM scale_assessment")
        remaining = cursor.fetchone()[0]
        print(f"剩余评估记录: {remaining} 条")
        
        if remaining == 0:
            print("\n[OK] 所有评估数据已成功清空！")
            print("  - 删除了 {} 条评估记录".format(assessment_count))
            print("  - 删除了 {} 条HAMD记录".format(hamd_count))
            print("  - 删除了 {} 条HAMA记录".format(hama_count))
            print("  - 删除了 {} 条YMRS记录".format(ymrs_count))
            print("  - 已重置所有表的自增ID\n")
        else:
            print(f"\n[WARNING] 清理可能不完整，还有 {remaining} 条记录")
        
        cursor.close()
        connection.close()
        
    except Exception as e:
        print(f"\n[ERROR] 清理失败: {e}")
        sys.exit(1)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("\n\n[!] 操作已取消")
        sys.exit(0)

